# @Time    : 2021/5/31 10:06
# @Author  : name
import time
import hmac
import hashlib
import base64
import urllib.parse
import requests
import json

BASEURL = "https://oapi.dingtalk.com/robot/send"
ACCESS_TOKEN = "b8f316ccd71d13b8ed4f84e2174dde15293b87030af442a0730186be4c984426"


def signature():
    timestamp = str(round(time.time() * 1000))
    secret = 'SEC7a5f63067cf12eb53e9c8444350af83e0591eebd1445ad86de800da9fdf4d575'
    secret_enc = secret.encode('utf-8')
    string_to_sign = '{}\n{}'.format(timestamp, secret)
    string_to_sign_enc = string_to_sign.encode('utf-8')
    hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest()
    sign = urllib.parse.quote_plus(base64.b64encode(hmac_code))
    return sign


def webhook(params):
    timestamp = str(round(time.time() * 1000))
    headers = {"Content-Type": "application/json"}
    url = BASEURL + "?access_token=%s" % ACCESS_TOKEN + "&timestamp=%s" % timestamp + "&sign=%s" % signature()
    response = requests.post(url=url, data=json.dumps(params), headers=headers)
    try:
        if response.status_code == 200:
            return response.json()
    except requests.RequestException as e:
        return e


if __name__ == '__main__':
    Text = {
        "msgtype": "text",
        "text": {
            "content": "我就是我, @XXX 是不一样的烟火"
        },
    }

    Link = {
        "msgtype": "link",
        "link": {
            "text": "这个即将发布的新版本，创始人xx称它为红树林。而在此之前，每当面临重大升级，产品经理们都会取一个应景的代号，这一次，为什么是红树林",
            "title": "时代的火车向前开",
            "picUrl": "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fa3.att.hudong.com%2F61%2F98%2F01300000248068123885985729957.jpg&refer=http%3A%2F%2Fa3.att.hudong.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1625021598&t=650de5b36bc5309c2c9b7f0ac7be8db5",
            "messageUrl": "https://www.dingtalk.com/s?__biz=MzA4NjMwMTA2Ng==&mid=2650316842&idx=1&sn=60da3ea2b29f1dcc43a7c8e4a7c97a16&scene=2&srcid=09189AnRJEdIiWVaKltFzNTw&from=timeline&isappinstalled=0&key=&ascene=2&uin=&devicetype=android-23&version=26031933&nettype=WIFI"
        }
    }
    MarkDown = {
        "msgtype": "markdown",
        "markdown": {
            "title": "杭州天气",
            "text": "#### 杭州天气 @150XXXXXXXX \n > 9度，西北风1级，空气良89，相对温度73%\n > ![screenshot](https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png)\n > ###### 10点20分发布 [天气](https://www.dingtalk.com) \n"
        },
        # "at": {
        #     "atMobiles": [
        #         "150XXXXXXXX"
        #     ],
        #     "atUserIds": [
        #         "user123"
        #     ],
        #     "isAtAll": False
        # }
    }
    ActionCardDate = {
        "msgtype": "actionCard",
        "actionCard": {
            "title": "乔布斯 20 年前想打造一间苹果咖啡厅，而它正是 Apple Store 的前身",
            "text": "![screenshot](https://gw.alicdn.com/tfs/TB1ut3xxbsrBKNjSZFpXXcXhFXa-846-786.png)### 乔布斯 20 年前想打造的苹果咖啡厅Apple Store 的设计正从原来满满的科技感走向生活化，而其生活化的走向其实可以追溯到 20 年前苹果一个建立咖啡馆的计划",
            "btnOrientation": "0",
            "singleTitle": "阅读全文",
            "singleURL": "https://www.dingtalk.com/"
        }
    }

    DumpActionCard = {
        "mistype": "actionCard",
        "actionCard": {
            "title": "我 20 年前想打造一间苹果咖啡厅，而它正是 Apple Store 的前身",
            "text": "![screenshot](https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png) \n\n #### 乔布斯 20 年前想打造的苹果咖啡厅 \n\n Apple Store 的设计正从原来满满的科技感走向生活化，而其生活化的走向其实可以追溯到 20 年前苹果一个建立咖啡馆的计划",
            "hideAvatar": "0",
            "btnOrientation": "0",
            "btns": [
                {
                    "title": "内容不错",
                    "actionURL": "https://www.dingtalk.com/"
                },
                {
                    "title": "不感兴趣",
                    "actionURL": "https://www.dingtalk.com/"
                }
            ]
        }
    }

    FeedCard = {
        "msgtype": "feedCard",
        "feedCard": {
            "links": [
                {
                    "title": "时代的火车向前开1",
                    "messageURL": "https://www.dingtalk.com/",
                    "picURL": "https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png"
                },
                {
                    "title": "时代的火车向前开2",
                    "messageURL": "https://www.dingtalk.com/",
                    "picURL": "https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png"
                }
            ]
        }
    }
    resp = webhook(FeedCard)
    print(resp)

